package com.canbot.u05.activity.training;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.utils.ToastTools;

/**
 * 培训主持模式-------选择动作界面
 */

public class TrainingChooseActionActivity extends BaseActivityNoTitle {

        private GridView mGridviewHead, mGridviewBoot, mGridviewArm;

        private TrainingGridViewAdapter mAdapterHead, mAdapterBoot, mAdapterArm;

        private int mCurrentView, mCurrentPos = -1;

        private Button mButtonHead, mButtonBoot, mButtonArm;

        private String actionID = null;

        private String[] mHead;

        private String[] mArm;

        private String[] mBoot;

        private String[] mHeadId;

        private String[] mArmId;

        private String[] mBootId;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_training_host_choose_action);
                mButtonHead = (Button) findViewById(R.id.head);
                mButtonBoot = (Button) findViewById(R.id.boot);
                mButtonArm = (Button) findViewById(R.id.arm);
                mGridviewHead = (GridView) findViewById(R.id.gridview_head);
                mGridviewBoot = (GridView) findViewById(R.id.gridview_boot);
                mGridviewArm = (GridView) findViewById(R.id.gridview_arm);
                mGridviewHead.setSelector(new ColorDrawable(Color.TRANSPARENT));//取消GridView中Item选中时默认的背景色
                mGridviewBoot.setSelector(new ColorDrawable(Color.TRANSPARENT));//取消GridView中Item选中时默认的背景色
                mGridviewArm.setSelector(new ColorDrawable(Color.TRANSPARENT));//取消GridView中Item选中时默认的背景色

                mHead = getResources().getStringArray(R.array.head_text);
                mArm = getResources().getStringArray(R.array.arm_text);
                mBoot = getResources().getStringArray(R.array.boot_text);
                mHeadId = getResources().getStringArray(R.array.head_id);
                mArmId = getResources().getStringArray(R.array.arm_id);
                mBootId = getResources().getStringArray(R.array.boot_id);

                setAdapter();
                updateAdapterWithID(getIntent().getStringExtra("actionId"));
                switchGridview(0);

        }

        private void updateAdapterWithID(String extra) {
                Log.e("updateAdapterWithID", extra + "extra");
                for (int i = 0; i < mHeadId.length; i++) {
                        if (mHeadId[i].equals(extra)) {
                                Log.e("updateAdapterWithID", mHeadId + "extra");
                                mCurrentView = 0;
                                mCurrentPos = i;
                                mAdapterHead.updateSelect(i);
                                return;
                        }
                }
                for (int i = 0; i < mBootId.length; i++) {
                        if (mBootId[i].equals(extra)) {
                                mCurrentView = 1;
                                mCurrentPos = i;
                                mAdapterBoot.updateSelect(i);
                                return;
                        }
                }
                for (int i = 0; i < mArmId.length; i++) {
                        if (mArmId[i].equals(extra)) {
                                mCurrentView = 2;
                                mCurrentPos = i;
                                mAdapterArm.updateSelect(i);
                                return;
                        }
                }
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        private AdapterView.OnItemClickListener listenter = new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mCurrentPos = position;
                        if (mCurrentView == 0) {
                                mAdapterHead.updateSelect(position);
                        }
                        else if (mCurrentView == 1) {
                                mAdapterBoot.updateSelect(position);
                        }
                        else if (mCurrentView == 2) {
                                mAdapterArm.updateSelect(position);
                        }
                }
        };

        private void setAdapter() {
                mAdapterHead = new TrainingGridViewAdapter(mHead, mHeadId, this);
                mGridviewHead.setOnItemClickListener(listenter);
                mGridviewHead.setAdapter(mAdapterHead);

                mAdapterBoot = new TrainingGridViewAdapter(mBoot, mBootId, this);
                mGridviewBoot.setOnItemClickListener(listenter);
                mGridviewBoot.setAdapter(mAdapterBoot);

                mAdapterArm = new TrainingGridViewAdapter(mArm, mArmId, this);
                mGridviewArm.setOnItemClickListener(listenter);
                mGridviewArm.setAdapter(mAdapterArm);
        }

        private void switchGridview(int item) {
                mCurrentView = item;
                if (item == 0) {
                        mButtonHead.setSelected(true);
                        mButtonBoot.setSelected(false);
                        mButtonArm.setSelected(false);
                        mGridviewHead.setVisibility(View.VISIBLE);
                        mGridviewBoot.setVisibility(View.INVISIBLE);
                        mGridviewArm.setVisibility(View.INVISIBLE);
                }
                else if (item == 1) {
                        mButtonArm.setSelected(true);
                        mButtonBoot.setSelected(false);
                        mButtonHead.setSelected(false);
                        mGridviewBoot.setVisibility(View.VISIBLE);
                        mGridviewHead.setVisibility(View.INVISIBLE);
                        mGridviewArm.setVisibility(View.INVISIBLE);
                }
                else if (item == 2) {
                        mButtonBoot.setSelected(true);
                        mButtonHead.setSelected(false);
                        mButtonArm.setSelected(false);
                        mGridviewArm.setVisibility(View.VISIBLE);
                        mGridviewHead.setVisibility(View.INVISIBLE);
                        mGridviewBoot.setVisibility(View.INVISIBLE);
                }
        }

        public void head(View v) {
                switchGridview(0);
        }

        public void boot(View v) {
                switchGridview(1);
        }

        public void arm(View v) {
                switchGridview(2);
        }

        public void back(View v) {
                finish();
        }

        public void finish(View v) {
                if (mCurrentPos != -1) {
                        if (mCurrentView == 0) {
                                actionID = mHeadId[mCurrentPos];
                        }
                        else if (mCurrentView == 1) {
                                actionID = mBootId[mCurrentPos];
                        }
                        else if (mCurrentView == 2) {
                                actionID = mArmId[mCurrentPos];
                        }
                }
                if (actionID == null) {
                        ToastTools.newInstance(this).showText(getString(R.string.please_select_action));
                        return;
                }
                Intent intent = new Intent();
                intent.putExtra("actionId", actionID);
                setResult(RESULT_OK, intent);
                finish();
        }

}
